package wqh.dao.impl;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import wqh.dao.WaterChannelDao;
import wqh.domain.WaterChannel;

import java.util.List;

/**
 * Created by devts on 2017/1/5.
 */
@Repository("waterChannelDao")
public class WaterChannelDaoImpl extends DaoImpl<WaterChannel> implements WaterChannelDao {

    @Override
    public WaterChannel findByChannelAndDeviceId(Integer channel_number, Long water_device_id) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "FROM WaterChannel AS w WHERE w.channel_number = :channel_number AND w.waterDevice.id = :device_id";
        Query query = session.createQuery(hql);
        query.setParameter("channel_number",channel_number);
        query.setParameter("device_id",water_device_id);
        return query.list().size() == 0 ? null : (WaterChannel)( query.list().get(0));
    }

    @Override
    public List<WaterChannel> findByGeoId(Long geo_id) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "FROM WaterChannel AS w WHERE w.geoLocation.id = :geo_id";
        Query query = session.createQuery(hql);
        query.setParameter("geo_id",geo_id);
        return query.list().size() == 0 ? null :  query.list();
    }
}
